package com.hsh.erp.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hsh.erp.domain.entity.Stock;
import com.hsh.erp.domain.vo.StockVO;
import com.hsh.erp.mapper.StockMapper;
import com.hsh.erp.service.StockService;
import com.hsh.erp.utils.MyBeanUtils;
import org.springframework.stereotype.Service;

/**
 * @author USER
 * @description 针对表【stock】的数据库操作Service实现
 * @createDate 2024-06-22 14:02:02
 */
@Service
public class StockServiceImpl extends ServiceImpl<StockMapper, Stock>
        implements StockService {


    /**
     * 根据仓库id+产品id，修改库存
     *
     * @param stockVO
     * @return
     */
    @Override
    public boolean updateByQuery(StockVO stockVO) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("repository_id", stockVO.getRepositoryId());
        updateWrapper.eq("product_id", stockVO.getProductId());
        updateWrapper.set("num", stockVO.getNum());

        return update(updateWrapper);
    }

    /**
     * 根据仓库id+产品id，
     * 增加或减少指定数量库存呢
     *
     * @param stockVO
     * @param num     正数是为增加库存，负数为减少库存
     * @return
     */
    @Override
    public boolean updateOrAddNumByQuery(StockVO stockVO) {

        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("repository_id", stockVO.getRepositoryId());
        updateWrapper.eq("product_id", stockVO.getProductId());
        updateWrapper.setSql("num = num +" + stockVO.getNum());
        boolean update = update(updateWrapper);
        boolean insert = false;
        if (!update) {
            stockVO.setNum(stockVO.getNum());
            insert = save(MyBeanUtils.copy(stockVO, Stock.class));
        }
        return update || insert;
    }
}




